import { _decorator, Component, Node, sys } from 'cc';
import { LV, NUMBER, PARTGRADE, WEAPONTYPE } from './GameConstant';
const { ccclass, property } = _decorator;

@ccclass('PrefsManager')
export class PrefsManager {

    private static _instance: PrefsManager = null;
    public static get Instance(): PrefsManager {
        if (!PrefsManager._instance) {
            PrefsManager._instance = new PrefsManager();
        }
        return PrefsManager._instance;
    }

    public userData = {
        "Gold": 100,
        "CurWeapon": WEAPONTYPE.手枪,
        "CurPendant": NUMBER.NUMBER1,
        "FillRange": 0,
        "CurLV": LV.LV1,
        "checkedItem": {
            "PF": NUMBER.NUMBER1,
            "GC": NUMBER.NUMBER1,
            "GS": NUMBER.NUMBER1,
        },
        "haveItem": {
            "PF": [NUMBER.NUMBER1],
            "GC": [NUMBER.NUMBER1],
            "GS": [NUMBER.NUMBER1],
        },
        "PartGrade": {
            "手枪": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],//消音器--枪托--弹夹--瞄准镜
            "UZI": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],
            "散弹枪": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],
            "汤姆逊": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],
            "突击步枪": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],
        },
        "PartIndex": {
            "手枪": [-1, -1, -1, -1], //消音器--枪托--弹夹--瞄准镜
            "UZI": [-1, -1, -1, -1],
            "散弹枪": [-1, -1, -1, -1],
            "汤姆逊": [-1, -1, -1, -1],
            "突击步枪": [-1, -1, -1, -1],
        },
        "Parts": [PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE1, PARTGRADE.GRADE1, PARTGRADE.GRADE0,
        PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0, PARTGRADE.GRADE0],
        "haveWeapon": [WEAPONTYPE.手枪],
        // "haveWeapon": [WEAPONTYPE.手枪, WEAPONTYPE.UZI, WEAPONTYPE.散弹枪, WEAPONTYPE.汤姆逊, WEAPONTYPE.突击步枪],
        "unluckGS": [NUMBER.NUMBER1],
    }

    private constructor() {
        // sys.localStorage.setItem('userData', JSON.stringify(this.userData));
        const value = sys.localStorage.getItem('userData');
        if (value) {
            //有记录就读取  --- 没记录就存入数据
            this.userData = JSON.parse(value);
        } else {
            console.log(`userData is not exist`);
            sys.localStorage.setItem('userData', JSON.stringify(this.userData));
        }
    }

    public saveData() {
        sys.localStorage.setItem('userData', JSON.stringify(this.userData));
    }
}


